package com.example.iread.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.iread.R;
import com.example.iread.activity.BookInfoActivity;
import com.example.iread.bean.BookBean;
import com.example.iread.exception.MyException;
import com.example.iread.util.HttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.MyViewHolder> {

    private final Context context;

    private final List<BookBean> list;

    public BookListAdapter(Context context, List<BookBean> list) {
        this.context = context;
        this.list = list;
    }

    Handler handler = new Handler(Looper.getMainLooper());

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_book, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflate);
        myViewHolder.view.setOnClickListener(v -> {
            int adapterPosition = myViewHolder.getAdapterPosition();
            BookBean bookBean = list.get(adapterPosition);
            Intent intent = new Intent(context, BookInfoActivity.class);
            intent.putExtra("bookId", bookBean.id);
            context.startActivity(intent);
        });
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        BookBean bookBean = list.get(position);
        holder.tv_book_name.setText(bookBean.bookName);
        holder.tv_book_author.setText(bookBean.author);
        Log.i("SoRiya", "onBindViewHolder: " + bookBean);
        new Thread(() -> {
            try {
                URL url = new URL(HttpUtil.getImageUrl(bookBean.cover));
                InputStream inputStream = url.openStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                handler.post(() -> holder.img_book_cover.setImageBitmap(bitmap));
            } catch (IOException e) {
                throw new MyException(500, e.getMessage());
            }
        }).start();

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        final View view;
        final ImageView img_book_cover;
        final TextView tv_book_name;
        final TextView tv_book_author;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            img_book_cover = itemView.findViewById(R.id.img_book_cover);
            tv_book_name = itemView.findViewById(R.id.tv_book_name);
            tv_book_author = itemView.findViewById(R.id.tv_book_author);
        }
    }

}
